package com.google.shop.product.service.impl.order;

import com.google.shop.product.common.to.CardTO;
import com.google.shop.product.common.utils.JwtUtils;
import com.google.shop.product.entity.card.Card;
import com.google.shop.product.entity.member.BuyerAddress;
import com.google.shop.product.entity.order.ShopOrder;
import com.google.shop.product.repository.card.CardRepository;
import com.google.shop.product.repository.member.BuyerAddressRepository;
import com.google.shop.product.repository.order.OrderRepository;
import com.google.shop.product.service.order.OrderService;
import com.google.shop.product.vo.order.AddressVO;
import com.google.shop.product.vo.order.OrderGroupVO;
import com.google.shop.product.vo.order.OrderHeaderInfoVO;
import com.google.shop.product.vo.order.ShopOrderListVO;
import com.google.shop.product.vo.order.ShopOrderVO;
import com.google.shop.product.vo.order.savelineskuvo.LineSkuVO;
import com.sun.xml.bind.v2.runtime.reflect.opt.Const;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;
//import org.apache.commons.configuration.DataConfiguration;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
/* loaded from: app.jar:BOOT-INF/classes/com/google/shop/product/service/impl/order/OrderImpl.class */
public class OrderImpl implements OrderService {
    @Autowired
    CardRepository cardRepository;
    @Autowired
    OrderRepository orderRepository;
    @Autowired
    BuyerAddressRepository addressRepository;

    @Override // com.google.shop.product.service.order.OrderService
    public void buy(String token, Integer addressId) {
        String buyerId = JwtUtils.getMemberIdByJwtToken(token);
        List<Card> cardList = this.cardRepository.findCardsByBuyerIdAndAndCardSkuIsopt(Integer.valueOf(buyerId), 0);
        String cardOrderId = new Date().getTime() + "" + new Random().nextInt(10) + "" + new Random().nextInt(10) + "" + new Random().nextInt(10) + "" + new Random().nextInt(10);
        String cardOrderTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
//        this.orderRepository.saveAll((Iterable) ((List) cardList.stream().map(s -> {
//            return new ShopOrder(null, cardOrderId, 1, cardOrderTime, s.getCardSpuTitle(), s.getCardSkuPrice(), s.getCardSkuTag(), s.getCardSkuNum(), s.getCardSkuFreight(), BigDecimal.valueOf(Double.valueOf((double) s.getCardSkuAmount().intValue()).doubleValue() / 100.0d), s.getCardSkuImage(), s.getBuyerId(), addressId);
//        }).collect(Collectors.toList())));

        List<ShopOrder> orderList = cardList.stream().map(s -> new ShopOrder(
                null,
                cardOrderId,
                1,
                cardOrderTime,
                s.getCardSpuTitle(),
                s.getCardSkuPrice(),
                s.getCardSkuTag(),
                s.getCardSkuNum(),
                s.getCardSkuFreight(),
                BigDecimal.valueOf(Double.valueOf(s.getCardSkuAmount()) / 100),
                s.getCardSkuImage(),
                s.getBuyerId(),
                addressId
        )).collect(Collectors.toList());
        //购物车保存到订单表
        orderRepository.saveAll(orderList);

        this.cardRepository.deleteByBuyerIdAndCardSkuIsopt(Integer.valueOf(buyerId), 0);
    }


    @Override // com.google.shop.product.service.order.OrderService
    public List<ShopOrderListVO> list(String buyerId, String token) {
        List<ShopOrder> orderList = this.orderRepository.findByBuyerId(Integer.valueOf(buyerId));
        Map<String, Long> group = (Map) orderList.stream().collect(Collectors.groupingBy((v0) -> {
            return v0.getOrderId();
        }, Collectors.counting()));
        List<OrderGroupVO> list = new ArrayList<>();
        for (Map.Entry<String, Long> entry : group.entrySet()) {
            list.add(new OrderGroupVO(entry.getKey(), entry.getValue()));
        }
        List<OrderGroupVO> sortGroupList = (List) list.stream().sorted(Comparator.comparing((v0) -> {
            return v0.getOrderId();
        })).collect(Collectors.toList());
        Collections.reverse(sortGroupList);
        List<BuyerAddress> buyerAddressList = this.addressRepository.findByBuyerId(Integer.valueOf(JwtUtils.getMemberIdByJwtToken(token)));
        List<ShopOrderListVO> orderListVOS = new ArrayList<>();
        for (int i = 0; i < sortGroupList.size(); i++) {
            ShopOrderListVO orderListVO = new ShopOrderListVO();
            List<ShopOrderVO> orderVOList = new ArrayList<>();
            OrderHeaderInfoVO orderHeaderInfoVO = new OrderHeaderInfoVO();
            AddressVO addressVO = new AddressVO();
            Double orderAmount = Double.valueOf((double) Const.default_value_double);
            for (ShopOrder order : orderList) {
                ShopOrderVO orderVO = new ShopOrderVO();
                if (order.getOrderId().equals(sortGroupList.get(i).orderId)) {
                    BeanUtils.copyProperties(order, orderVO);
                    orderVO.setSkuPrice(order.getSkuPrice() + "");
                    orderVO.setSkuSaleAttrValues(order.getSkuSaleAttrValues().split(BeanFactory.FACTORY_BEAN_PREFIX));
                    orderVOList.add(orderVO);
                    BeanUtils.copyProperties(orderVO, orderHeaderInfoVO);
                    orderAmount = Double.valueOf(orderAmount.doubleValue() + (Double.valueOf(orderVO.getSkuPrice()).doubleValue() * ((double) orderVO.getSkuNum().intValue())) + ((double) orderVO.getFreight().intValue()));
                }
            }
            orderHeaderInfoVO.setOrderAmount(BigDecimal.valueOf(orderAmount.doubleValue()));
            for (BuyerAddress address : buyerAddressList) {
                if (address.getId() == orderVOList.get(0).getAddressId()) {
                    BeanUtils.copyProperties(address, addressVO);
                    addressVO.setBuyerReceiveDetailAddress(StringUtils.join(address.getBuyerReceiveDetailAddress().split(BeanFactory.FACTORY_BEAN_PREFIX), ""));
                }
            }
            orderListVO.setShopOrderVOList(orderVOList);
            orderListVO.setAddressVO(addressVO);
            orderListVO.setOrderHeaderInfoVO(orderHeaderInfoVO);
            orderListVO.setSotr(Integer.valueOf(i));
            orderListVOS.add(orderListVO);
        }
        return orderListVOS;
    }

    @Override // com.google.shop.product.service.order.OrderService
    public void saveLineSku(LineSkuVO lineSkuVO, String buyerId) {
        ShopOrder order = new ShopOrder();
        BeanUtils.copyProperties(lineSkuVO.getDetailVO(), order);
        order.setAddressId(lineSkuVO.getAddressId());
        order.setBuyerId(Integer.valueOf(buyerId));
        order.setOrderId(new Date().getTime() + "" + new Random().nextInt(10) + "" + new Random().nextInt(10) + "" + new Random().nextInt(10) + "" + new Random().nextInt(10));
        order.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        order.setOrderStatus(1);
        order.setSkuSaleAttrValues(StringUtils.join(lineSkuVO.getDetailVO().getSkuSaleAttrValues(), BeanFactory.FACTORY_BEAN_PREFIX));
        this.orderRepository.save(order);
    }
}
